package cn.yunhe.service;

import cn.yunhe.dao.NewsDao;
import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import cn.yunhe.util.DateUtil;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class NewsService {
    private SqlSession session = null;
    private NewsDao newsDao = null;
    private void init(){
        session = SqlSessionFactoryUtil.getSession();
        newsDao = session.getMapper(NewsDao.class);
    }

    private void destory(){
        session.close();
    }

    /**
     * by郭瀚钰
     * 发布新闻
     * @param news
     * @return
     */
    public boolean addNews(News news){
        init();
        boolean flag = newsDao.addNews(news);
        session.commit();
        destory();
        return flag;
    }

    /**
     * 删除新闻
     * @param enidList
     * @return
     */
    public  boolean delNews(List<Integer> enidList){
        init();
        boolean flag = newsDao.delNews(enidList);
        session.commit();
        destory();
        return flag;
    }

    /**
     * 修改新闻
     * @param news
     * @return
     */
    public  boolean upNews(News  news){
        init();
        boolean flag = newsDao.upNews(news);
        System.out.println(news.getEntitle());
        session.commit();
        destory();
        return flag;
    }

    /**
     * 根据id获取新闻信息
     * @param id
     * @return
     */
    public News getNewsById(int id){
        init();
        News news = newsDao.getNewsById(id);
        destory();
        return news;
    }

    /**
     * 分页获取促销信息
     * @param pageIndex
     * @param pageSize
     * @param keyword
     * @return
     */
    public List<News> getCuXiaoNews(int pageIndex,int pageSize,String keyword){
            init();
            int pageStart = (pageIndex-1)*pageSize;
            Page page =  new Page(pageStart,pageSize,keyword);
            List<News> list = new ArrayList<News>();
            list = newsDao.getCuXiaoNews(page);

            destory();
            return list;
    }

    /**
     * 分页获取公告信息
     * @param pageIndex
     * @param pageSize
     * @param keyword
     * @return
     */
    public List<News> getGongGaoNews(int pageIndex,int pageSize,String keyword){
        init();
        int pageStart = (pageIndex-1)*pageSize;
        Page page =  new Page(pageStart,pageSize,keyword);
        List<News> list = new ArrayList<News>();
        list = newsDao.getGongGaoNews(page);
        destory();
        return list;
    }

    /**
     * 获取促销信息总记录数
     * @param page
     * @return
     */
    public int getCuXiaoNewsCount(Page page){
        init();
        int num = newsDao.getCuXiaoNewsCount(page);
        destory();
        return  num;
    }

    /**
     * 获取公告信息总记录数
     * @param page
     * @return
     */
    public  int getGongGaoNewsCount(Page page){
        init();
        int num = newsDao.getGongGaoNewsCount(page);
        destory();
        return  num;
    }



}
